package com.hopu.v1.controller.admin.system;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.common.PageData;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.system.Staff;
import com.hopu.v1.service.admin.system.StaffService;
import com.hopu.v1.vo.StaffVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YwaiX
 * @since 2024-09-12
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;


    /**
     * 获取员工列表
     *
     * @param page
     * @return
     */
    @PostMapping("selectAll")
    public R selectAll(Page<Staff> page, Staff staff) {
        PageData<StaffVo> pageData = staffService.selectAll(page, staff);
        return R.ok("获取成功", pageData);
    }

    /**
     * 登录
     *
     * @param staff
     * @return
     */
    @PostMapping("login")
    public R login(Staff staff) {
        return R.ok("登陆成功", this.staffService.login(staff));
    }

    /**
     * 修改员工
     *
     * @param staff 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(Staff staff) {
        staff.setUpdateTime(LocalDateTime.now());
        this.staffService.update(staff);
        return R.ok("更新成功");
    }

    /**
     * 添加员工
     *
     * @param staff
     * @return
     */
    @PostMapping("/addStaff")
    public R insert(Staff staff) {
        System.out.println(staff);
        staff.setJoinDate(LocalDate.now());
        staff.setCreateTime(LocalDateTime.now());
        staff.setStatus(1);
        this.staffService.add(staff);
        return R.ok("添加成功");
    }

    /**
     * 导出excel形式的用户信息
     * @throws IOException
     */
    @GetMapping("/downloadStaffs")
    public void exportUsers(HttpServletResponse response) throws IOException {
        // 从数据库获取用户列表
        List<Staff> staffs = staffService.list();
        staffService.exportUsersToExcel(staffs, response);
    }


    /**
     * 上传需要导入的excel表格
     * @param file
     * @return
     */
    @PostMapping("/excelUpload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file!= null &&!file.isEmpty()) {
            try {
                // 在这里调用EasyExcel读取文件的方法
                readExcel(file.getInputStream());
                return new ResponseEntity<>("文件上传并读取成功", HttpStatus.OK);
            } catch (IOException e) {
                e.printStackTrace();
                return new ResponseEntity<>("文件上传失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } else {
            return new ResponseEntity<>("请选择要上传的文件", HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 读取excel表格
     * @param inputStream
     */
    public void readExcel(InputStream inputStream) {

        // 使用EasyExcel进行读取
        EasyExcel.read(inputStream, Staff.class, new PageReadListener<Staff>(dataList ->{
            for (Staff staff : dataList) {
                insert(staff);
            }
        })).build().readAll();

        // 关闭输入流（注意要在读取完成后关闭）
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询员工信息
     *
     * @param staffNo 员工编号
     * @return 员工信息
     */
    @GetMapping("staffInfo")
    public R staffInfo(String staffNo) {
        return R.ok("查询成功", this.staffService.staffInfo(staffNo));
    }

    /**
     * 修改密码和或者手机号
     */
    @PostMapping("updateInfo")
    public R updateInfo(String staffNo, String password, String phone,String photoUrl,String realName,String gender) {
        this.staffService.updateInfo(staffNo, password, phone,photoUrl,realName,gender);
        return R.ok("修改成功");
    }

    /**
     * 修改员工状态
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("updateStatus")
    public R updateStatus(Integer id, Integer status) {
        System.out.println(id + "," + status);
        staffService.updateStatus(id, status);
        return R.ok("修改成功");
    }

    /**
     * 批量删除用户
     *
     * @param ids
     * @return
     */
    @GetMapping("/deleteStaff/{ids}")
    public R deleteStaff(@PathVariable List<Integer> ids) {
        staffService.deleteStaff(ids);
        return R.ok("操作成功");
    }
    /**
     * 查询员工信息
     *
     * @param id 员工编号
     * @return 员工信息
     */
    @GetMapping("staffInfoById")
    public R staffInfoById(String id) {
        return R.ok("查询成功", staffService.getOptById(id));
    }

    /**
     * 获取最新一条员工编号
     */
    @GetMapping("/selectStaffNo")
    public R selectStaffNo(String staffNo) {
        String lastStaffNo = staffService.selectStaffNo(staffNo);
        return R.ok(lastStaffNo);
    }

    @GetMapping("/getStaffsWithSubjects")
    public R getStaffsWithSubjects() {
        return R.ok(this.staffService.getStaffsWithSubjects());
    }

}
